package com.example.management.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.management.common.QueryPageParam;
import com.example.management.common.Result;
import com.example.management.entity.Inbuy;
import com.example.management.service.InbuyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/inbuy")
public class InbuyController {
    @Autowired
    private InbuyService inbuyService;
    //查询
    @GetMapping("/list")
    public List<Inbuy> list() { return inbuyService.list(); }
    //新增
    @PostMapping("/save")
    public Result save(@RequestBody Inbuy inbuy) {
        return inbuyService.save(inbuy) ? Result.success() : Result.fail();
    }
    @PostMapping("/update")
    public Result update(@RequestBody Inbuy inbuy) {
        return inbuyService.updateById(inbuy) ? Result.success() : Result.fail();
    }
    //修改
    @PostMapping("/mod")
    public boolean mod(@RequestBody Inbuy inbuy) {
        return inbuyService.updateById(inbuy);
    }
    //新增或修改
    @PostMapping("/saveOrMod")
    public boolean saveOfMod(@RequestBody Inbuy inbuy) {
        return inbuyService.saveOrUpdate(inbuy);
    }
    //删除
    @GetMapping("/delete")
    public Result delete(String ino) {
        return inbuyService.removeById(ino) ? Result.success() : Result.fail();
    }
    @GetMapping("/findByNo")
    public Result findByNo(String ino) {
        List<Inbuy> list = inbuyService.lambdaQuery().eq(Inbuy::getIno, ino).list();
        return list.isEmpty() ? Result.fail() : Result.success();
    }
    @GetMapping("/findByEno")
    public Result findByEno(String eno) {
        List<Inbuy> list = inbuyService.lambdaQuery().eq(Inbuy::getEno, eno).list();
        return list.isEmpty() ? Result.fail() : Result.success();
    }
    //查询（模糊、匹配）
    @PostMapping("/listP")
    public List<Inbuy> listP(@RequestBody Inbuy inbuy) {
        LambdaQueryWrapper<Inbuy> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(Inbuy::getIno, inbuy.getIno());
        return inbuyService.list(lambdaQueryWrapper);
    }
    @PostMapping("/listPage")
    public Result listPage(@RequestBody QueryPageParam query) {
        System.out.println(query);
        System.out.println("num==" + query.getPageNum());
        System.out.println("size==" + query.getPageSize());
        HashMap param = query.getParam();
        String ino = (String) param.get("ino");
        String eno = (String) param.get("eno");

        Page<Inbuy> page = new Page<>(query.getPageNum(), query.getPageSize());
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());

        LambdaQueryWrapper<Inbuy> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(StringUtils.isNotBlank(ino) && !"null".equals(ino)) {
            lambdaQueryWrapper.eq(Inbuy::getIno, ino);
        }
        if(StringUtils.isNotBlank(eno) && !"null".equals(eno)) {
            lambdaQueryWrapper.eq(Inbuy::getEno, eno);
        }
        IPage<Inbuy> data = inbuyService.page(page, lambdaQueryWrapper);
        System.out.println("total==" + data.getTotal());

        return Result.success(data.getRecords(), data.getTotal());
    }
}